package com.gltqe.mask;

import java.util.Collections;

/**
 * 脱敏工具类
 *
 * @author gltqe
 * @date 2023/6/17 19:32
 */
public class DataMaskUtil {

    /**
     * 脱敏姓名，只保留姓氏
     *
     * @param name 姓名
     * @return 脱敏后的姓名
     */
    public static String maskName(String name) {
        if (name == null || name.isEmpty()) {
            return "";
        }
        return name.substring(0, 1) + String.join("", Collections.nCopies(name.length() - 1, "*"));
    }

    /**
     * 脱敏手机号，保留前三位和后四位
     *
     * @param phone 手机号
     * @return 脱敏后的手机号
     */
    public static String maskPhone(String phone) {
        if (phone == null || phone.isEmpty()) {
            return "";
        }
        return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
    }

    /**
     * 脱敏身份证号码，保留前四位和后四位
     *
     * @param idCard 身份证号码
     * @return 脱敏后的身份证号码
     */
    public static String maskIdCard(String idCard) {
        if (idCard == null || idCard.isEmpty()) {
            return "";
        }
        return idCard.replaceAll("(\\d{4})\\d+(\\w{4})", "$1**********$2");
    }

    /**
     * 脱敏银行卡号，保留前四位和后四位
     *
     * @param bankCard 银行卡号
     * @return 脱敏后的银行卡号
     */
    public static String maskBankCard(String bankCard) {
        if (bankCard == null || bankCard.isEmpty()) {
            return "";
        }
        return bankCard.replaceAll("(\\d{4})\\d+(\\d{4})", "$1**********$2");
    }

    /**
     * 脱敏电子邮箱，保留@前面的第一个字符和@后面的域名
     *
     * @param email 电子邮箱
     * @return 脱敏后的电子邮箱
     */
    public static String maskEmail(String email) {
        Integer partsLength = 2;
        if (email == null || email.isEmpty()) {
            return "";
        }
        String[] parts = email.split("@");
        if (parts.length != partsLength) {
            return email;
        }
        return parts[0].substring(0, 1) + "****@" + parts[1];
    }

    /**
     * 脱敏地址信息，保留前三个字符和后两个字符
     *
     * @param address 地址信息
     * @return 脱敏后的地址信息
     */
    public static String maskAddress(String address) {
        Integer addressLength = 5;
        if (address == null || address.isEmpty()) {
            return "";
        }
        if (address.length() <= addressLength) {
            return address;
        }
        return address.substring(0, 3) + "**" + address.substring(address.length() - 2);
    }

    /**
     * 脱敏IP地址，保留前三位和后一位
     *
     * @param ipAddress IP地址
     * @return 脱敏后的IP地址
     */
    public static String maskIpAddress(String ipAddress) {
        Integer partsLength = 4;
        if (ipAddress == null || ipAddress.isEmpty()) {
            return "";
        }
        String[] parts = ipAddress.split("\\.");
        if (parts.length != partsLength) {
            return ipAddress;
        }
        return parts[0] + "." + parts[1] + "." + parts[2] + ".*";
    }

    /**
     * 默认脱敏
     *
     * @return String
     * @author gltqe
     * @date 2023/6/18 1:58
     */
    public static String defaultMask(String data) {
        if (data == null || data.isEmpty()) {
            return "";
        }

        if (data.length() == 1) {
            return data.charAt(0) + "*";
        } else {
            return data.charAt(0) + "******";
        }
    }
}
